package com.springcloudserver.config;

import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;


/**
 * @description:
 * @author: chenzuolin
 * @time: 2021/3/20 22:12
 */
@Configuration
public class CaCheableConfig {

    @Bean("myKeyGenerator")
    public KeyGenerator keyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object o, Method method, Object... objects) {
                String class_ = o.getClass().toString().replace("class ", "");
                List<Object> args = Arrays.asList(objects);
                if (CollectionUtils.isEmpty(args)) {
                    return class_ + "." + method.getName();
                } else {
                    return class_ + "." + method.getName() + ":" + args;
                }
            }
        };
    }
}
